//
//  ShopHome2Controller.m
//  card
//
//  Created by 韦显光 on 2017/12/27.
//  Copyright © 2017年 xg韦. All rights reserved.
//

#import "ShopHome2Controller.h"
#import "CollCell.h"
#import "CollHerad.h"
#import "ShopHomeCell.h"
#import "Collfoot.h"
#import "TitleCollCell.h"

@interface ShopHome2Controller ()
{
    NSArray*tbvary;
    NSIndexPath*selidx;
   
}
@property (weak, nonatomic) IBOutlet UICollectionView *collview;
@property (weak, nonatomic) IBOutlet UITableView *tbv;
@property(assign,nonatomic) BOOL isScrollDown;
- (IBAction)searchBtna:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *hidenview;

@end

@implementation ShopHome2Controller
- (IBAction)SHback:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    [_tbv setTableFooterView:v];
     _tbv.separatorStyle = UITableViewCellSeparatorStyleNone;
    selidx=[NSIndexPath indexPathForRow:0 inSection:0];
    [self loaddata];

    static NSString *ID = @"TitleCollCell";
    [_collview registerClass:[TitleCollCell class] forCellWithReuseIdentifier:ID];

}
-(void)loaddata
{
    [FuncPublic getData:self parameters:nil RequestUrl:Ccate_list success:^(NSDictionary *acountInfo) {
        tbvary=acountInfo[@"cates_list"];
        [_collview reloadData];
         [self TbvLinkage];
        
    }];
}

-(void)loadCollviewdata
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -- UICollectionViewDataSource 数据源方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

      return CGSizeMake(DEVW/4.5 ,38);
 
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return tbvary.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSArray*temp=tbvary[section][@"childs"];
    return temp.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
      NSArray*temp=tbvary[indexPath.section][@"childs"];
      NSDictionary*dic=temp[indexPath.row];

          static NSString *ID = @"TitleCollCell";
          TitleCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
          [cell.btno setBackgroundColor:[UIColor whiteColor]];
          [cell.btno setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
          cell.btno.borderWidth=0.5;
          cell.btno.borderColor=[UIColor lightGrayColor];
           [cell.btno setTitle:dic[@"name"] forState:UIControlStateNormal];
          [cell.btno titleLabel].font=[UIFont systemFontOfSize:12];
           return cell;
      

}

#pragma mark --UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary*dic=tbvary[indexPath.section][@"childs"][indexPath.row];
    if ([_type isEqualToString:@"home"]) {
        [MyUtils collviewSelidex:@"兼职" toVc:self andShopInfo:dic];
    }else
    {
        self.completion(dic);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

//四周的边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//设置垂直间距,默认的垂直和水平间距都是10
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}
//设置水平间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

//设置header和footer的view
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (kind == UICollectionElementKindSectionFooter){
        Collfoot*foot=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Collfoot" forIndexPath:indexPath];

        return foot;
    }else{
    
        CollHerad*foot=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollHerad" forIndexPath:indexPath];
        foot.lb1.text=tbvary[indexPath.section][@"name"];
        return foot;
    }
    
}

// 获取CollectionView的滚动方向，是向上还是向下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    static CGFloat lastOffsetY = 0;
    
    if (_collview == scrollView) {
        _isScrollDown = lastOffsetY < scrollView.contentOffset.y;
        lastOffsetY = scrollView.contentOffset.y;
    }
}

// CollectionView分区标题即将展示
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    // 当前CollectionView滚动的方向向上，CollectionView是用户拖拽而产生滚动的（主要是判断CollectionView是用户拖拽而滚动的，还是点击TableView而滚动的）
    if (!_isScrollDown && collectionView.dragging) {
        [self selectRowAtIndexPath:indexPath.section];
    }
}

// CollectionView分区标题展示结束
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(nonnull UICollectionReusableView *)view forElementOfKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath {
    
    // 当前CollectionView滚动的方向向下，CollectionView是用户拖拽而产生滚动的（主要是判断CollectionView是用户拖拽而滚动的，还是点击TableView而滚动的）
    if ( _isScrollDown && collectionView.dragging) {
        [self selectRowAtIndexPath:indexPath.section + 1];
    }
}

// 当拖动CollectionView的时候，处理TableView
- (void)selectRowAtIndexPath:(NSInteger)index {
    selidx=[NSIndexPath indexPathForRow:index inSection:0];
    [_tbv selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    [_tbv reloadData];
    
}



//////////////////////////////////////////////////////////////////////////////////////////

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  tbvary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    ShopHomeCell*cell=[ShopHomeCell CellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lb1.text=tbvary[indexPath.row][@"name"];
    
    if (indexPath==selidx) {
        [cell.lb1 setFont:[UIFont systemFontOfSize:16]];
         cell.hidenLine.hidden=NO;
        cell.hidenLine.backgroundColor=[MyUtils randomColor];
        [cell.contentView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }else
    {
        [cell.lb1 setFont:[UIFont systemFontOfSize:14]];
        cell.hidenLine.hidden=YES;
        [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    }
    return cell;

    
}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSString*tempstr=WXGStr(tbvary[indexPath.row][@"name"]);
    selidx=indexPath;
    [self TbvLinkage];
    
}
-(void)TbvLinkage
{
    [_tbv reloadData];
    NSIndexPath *collectionIndexPath = [NSIndexPath indexPathForItem:0 inSection:selidx.row];
    // 获取Section对应CollectionView布局属性
    UICollectionViewLayoutAttributes *attributes = [_collview layoutAttributesForItemAtIndexPath:collectionIndexPath];
    // 设置CollectionView滚动位置

         [_collview setContentOffset:CGPointMake(0, attributes.frame.origin.y-40) animated:YES];

}




@end
